package com.definity.toolkit.web.taglib;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;

public class RadioListTag extends ComponentFormTag {

	private static final long serialVersionUID = 1720907603114519882L;

	private String list;
	private String itemLabel;
	private String itemValue;
	
	public RadioListTag() {

	}
	
	public void setList(String list) {
		this.list = list;
	}
	
	public void setItemLabel(String itemLabel) {
		this.itemLabel = itemLabel;
	}
	
	public void setItemValue(String itemValue) {
		this.itemValue = itemValue;
	}
	
	@Override
	protected String getComponentName() {
		return "radio-list";
	}

	@Override
	protected String createLabel(String id, String label, Object value, FormTag formTag) {
		return "";
	}
	
	@Override
	protected String createStartTag(String name, String id, Object value) throws JspException {
		return null;
	}

	@Override
	protected String createEndTag(String name, String id, Object value) throws JspException {
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
		
		List<?> domainList = (List<?>) request.getAttribute(list);
		
		StringBuilder html = new StringBuilder();
		html.append("<ul " + createStyleClass("radio-list") + " id=\"" + id + "\">");
		
		for (Object domain : domainList) {
			Object radioValue = getValue(domain, itemValue);
			
			String checked = "";
			if (value != null && value.equals(radioValue))
				checked = "checked";
			
			html.append("<li>");
			html.append("<input type=\"radio\" name=\"" + id + "\" value=\"" + radioValue + "\" " + checked + " />");

			html.append("<label>");
			html.append(getValue(domain, itemLabel));
			html.append("</label>");
			html.append("</li>");
		}
		html.append("</ul>");
		
		return html.toString();
	}

}
